package od_2025A.one;


import java.util.*;

/**
 * time:21.45
 * https://blog.csdn.net/qq_45776114/article/details/145763828
 * 题目分析
 * 0、读取两行数据
 * 1、第一行为消息发送的时刻和内容
 * 2、第二行为消费者订阅时间和取消时间
 * 3、打印出，依次打印出每个消费者收到的消息内容
 * TODO 代码可优化的点很多,从空间,从时间等各方面都可以进行优化,但是优化之后,感觉没现在易懂
 * 注意:要求打印的消息内容也是按照顺序来的,这就是为啥第一次没过的原因
 */
public class Test05 {

    static class Event {
        private int time;
        private int content;
        private boolean sub;

        public Event(int time, int content) {
            this.time = time;
            this.content = content;
            this.sub = false;
        }

        public int getTime() {
            return time;
        }

        public void setTime(int time) {
            this.time = time;
        }

        public int getContent() {
            return content;
        }

        public void setContent(int content) {
            this.content = content;
        }

        public boolean isSub() {
            return sub;
        }

        public void setSub(boolean sub) {
            this.sub = sub;
        }
    }

    static class Customer {
        private int priority;
        private int subTime;
        private int cancelTime;
        private String content = "-1";

        public Customer(int priority, int subTime, int cancelTime) {
            this.priority = priority;
            this.subTime = subTime;
            this.cancelTime = cancelTime;
        }

        public int getPriority() {
            return priority;
        }

        public void setPriority(int priority) {
            this.priority = priority;
        }

        public int getSubTime() {
            return subTime;
        }

        public void setSubTime(int subTime) {
            this.subTime = subTime;
        }

        public int getCancelTime() {
            return cancelTime;
        }

        public void setCancelTime(int cancelTime) {
            this.cancelTime = cancelTime;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }
    }

    public static void main(String[] args) {
        // 1.读取控制台数据
        Scanner scanner = new Scanner(System.in);
        String eventStr = scanner.nextLine();
        String customerStr = scanner.nextLine();
        scanner.close();
        // 2.构建消息队列 (todo 这里如果用队列是什么样子,消息如何按照时间进行排序)
        ArrayList<Event> eventList = new ArrayList<Event>();
        String[] eventArray = eventStr.split(" ");
        for (int i = 0; i < eventArray.length; i = i + 2) {
            Event event = new Event(Integer.parseInt(eventArray[i]), Integer.parseInt(eventArray[i + 1]));
            eventList.add(event);
        }
        eventList.sort(Comparator.comparingInt(Event::getTime));
        // 3.构建消费者集合
        String[] customerArray = customerStr.split(" ");
        ArrayList<Customer> customerList = new ArrayList<Customer>();
        int priority = 0;
        for (int i = 0; i < customerArray.length; i = i + 2) {
            Customer customer = new Customer(priority++, Integer.parseInt(customerArray[i]), Integer.parseInt(customerArray[i + 1]));
            customerList.add(customer);
        }
        customerList.sort(Comparator.comparingInt(Customer::getPriority).reversed());
        // 4.遍历消费者集合,和 消息队列,打印消费者收到的消息
        for (Customer customer : customerList) {
            StringBuilder builder = new StringBuilder();
            for (Event event : eventList) {
                if (event.getTime() >= customer.getSubTime() && event.getTime() < customer.getCancelTime() && !event.isSub()) {
                    builder.append(event.getContent()).append(" ");
                    event.setSub(true);
                }
            }
            if (builder.length() != 0) {
                customer.setContent(builder.substring(0, builder.length() - 1));
            }
        }
        customerList.sort(Comparator.comparingInt(Customer::getPriority));
        for (Customer customer : customerList) {
            System.out.println(customer.getContent());
        }
    }
}
